package com.geofitti.andoird;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Base64;
import android.widget.LinearLayout;



import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MapViews extends MapActivity{
	
	LinearLayout linearLayout;
	MapView mapView;
	List<Overlay> mapOverlays;
	Drawable drawable;
	ItemizedOverlays itemizedOverlay;

	double [] weber = {40.687786, -111.89807};
	double [] disneyland = {33.8093820, -117.9153920};
	double [] zion = {37.2999812, -113.0507768};
	double [] rushmore = {43.880154, 43.880154};
	double [] keys = {24.7433195, -81.2518833};
	double [] latlng6 = {40.687786, -111.89807}; //slcc
	double [] latlng7 = {41.1925176, -111.9446647}; // weber
	double [] latlng8 = {40.7649252, -111.8481159}; // UofU

	
	GeoPoint point = new GeoPoint(19240000,-99120000);
	GeoPoint point2 = new GeoPoint(35410000, 139460000);
	GeoPoint point3 = coordinatesToGeoPoint(disneyland);
	GeoPoint point4 = coordinatesToGeoPoint(zion);
	GeoPoint point5 = coordinatesToGeoPoint(rushmore);
	GeoPoint point6 = coordinatesToGeoPoint(keys);
	GeoPoint point7 = coordinatesToGeoPoint(latlng6);
	GeoPoint point8 = coordinatesToGeoPoint(latlng7);
	GeoPoint point9 = coordinatesToGeoPoint(latlng8);


	//the second and third inputs take strings and display text when we click our marker,
	//because we haven't yet written the click handler for the OverlayItem.
	
	OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
	OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");
	OverlayItem overlayitem3 = new OverlayItem(point3, "sunset", "Stuff"); //disneyland
	OverlayItem overlayitem4 = new OverlayItem(point4, "todizzle", ""); //zions
	OverlayItem overlayitem5 = new OverlayItem(point5, "fireworks", ""); //rushmore
	OverlayItem overlayitem6 = new OverlayItem(point6, "booth", ""); //keys
	OverlayItem overlayitem7 = new OverlayItem(point7, "slcc", "");
	OverlayItem overlayitem8 = new OverlayItem(point8, "weber", "");
	OverlayItem overlayitem9 = new OverlayItem(point9, "uofu", "");
	
	
	 public void onCreate(Bundle savedInstanceState) {
//testings
//		 Bitmap someimg = null;
//	      
//	       JSONObject jsonO;
//	       try {
//	           jsonO = WebCall.doGet("http://icarus.cs.weber.edu/~km06559/geofitti.php/getImage");
////	           final String status = jsonO.getString("status");
////	          
////	           byte[] decodedString;
////	           decodedString = Base64.decode(jsonO.getString("photo"), Base64.DEFAULT);
////	           someimg = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
//
//	       } catch (JSONException e) {
//	           // TODO Auto-generated catch block
//	           e.printStackTrace();
//	       }
		 
//end testing
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.geofitti_mapview);
	        
	        MapView mapView = (MapView) findViewById(R.id.mapview);
	        mapView.setBuiltInZoomControls(true);
	        
	        List<Overlay> mapOverlays = mapView.getOverlays();
	        Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
	        itemizedOverlay = new ItemizedOverlays(drawable, this);
	        itemizedOverlay.addOverlay(overlayitem, "boo");
	        itemizedOverlay.addOverlay(overlayitem2, "urns");
	        itemizedOverlay.addOverlay(overlayitem3, "boo1");
	        itemizedOverlay.addOverlay(overlayitem4, "urns1");
	        itemizedOverlay.addOverlay(overlayitem5, "boo2");
	        itemizedOverlay.addOverlay(overlayitem6, "urns2");
	        itemizedOverlay.addOverlay(overlayitem7, "urns1");
	        itemizedOverlay.addOverlay(overlayitem8, "boo2");
	        itemizedOverlay.addOverlay(overlayitem9, "urns2");

	        mapOverlays.add(itemizedOverlay);
	        
//	        itemizedOverlay.setImage(someimg);
	    }
	 
	    @Override
	    protected boolean isRouteDisplayed() {
	        return false;
	    }
	    
	    public static GeoPoint coordinatesToGeoPoint(double[] coords) {
	        if (coords.length > 2) {
	            return null;
	        }
	        if (coords[0] == Double.NaN || coords[1] == Double.NaN) {
	            return null;
	        }
	        final int latitude = (int) (coords[0] * 1E6);
	        final int longitude = (int) (coords[1] * 1E6);
	        return new GeoPoint(latitude, longitude);
	    }
}
